package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiDriverUuidParam;
import com.yungu.swift.common.model.vo.AppVersionVo;
import com.yungu.swift.common.service.CommonVersionService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.lbs.model.DriverPoint;
import com.yungu.swift.lbs.model.param.ApiAroundParam;
import com.yungu.swift.lbs.service.LocationService;
import com.yungu.swift.system.sys.model.dto.SysAirportDto;
import com.yungu.swift.system.sys.model.dto.SysCityDto;
import com.yungu.swift.system.sys.model.dto.SysCountryDto;
import com.yungu.swift.system.sys.model.vo.BusinessDetailVo;
import com.yungu.swift.system.sys.service.SysAirportService;
import com.yungu.swift.system.sys.service.SysBusinessCityService;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.system.sys.service.SysCountryService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;

@RestController
@RequestMapping(value = "/api/v2/passenger")
@Api(value = "【乘客】【通用】通用模块接口V2", tags = "V2-通用模块接口")
public class PasCommonApi {

    @Reference
    private LocationService locationService;
    @Reference
    private SysCountryService sysCountryService;
    @Reference
    private SysCityService sysCityService;
    @Reference
    private SysBusinessCityService sysBusinessCityService;
    @Reference
    private SysAirportService sysAirportService;
    @Reference
    private CommonVersionService commonVersionService;
    @Reference
    private DriverService driverService;

    @RequestMapping(value = {"/country/getCountryList"}, method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "获取国家列表", notes = "获取国家列表")
    public ResponseData<List<SysCountryDto>> countryList() {
        return sysCountryService.list(MapUtils.build());
    }

    @RequestMapping(value = {"/city/find"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取指定城市信息接口", notes = "获取指定城市信息接口")
    public ResponseData<SysCityDto> cityFind(@RequestBody Map<String, String> param, @RequestHeader String appid) {
        // 城市名
        String name = param.get("name");
        if (StringUtils.isEmpty(name)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        Map<String, Object> paramMap = MapUtils.build(4);
        paramMap.put("appid", appid);
        paramMap.put("city", name);
        return sysCityService.get(paramMap);
    }

    @RequestMapping(value = {"/sysBusinessCity/list"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取开通城市业务二维列表", notes = "城市业务二维列表")
    public ResponseData<List<BusinessDetailVo>> openList(@RequestHeader String appid) {
        return sysBusinessCityService.getBusinessDetail(MapUtils.build("organizationUuid", appid));
    }

    @PostMapping(value = {"/system/getAirport"})
    @ApiOperation(value = "获取城市机场列表", notes = "获取城市机场列表")
    public ResponseData<List<SysAirportDto>> getAirport(@RequestBody Map<String, String> params) {
        // 城市名
        String cityName = params.get("cityName");
        if (StringUtils.isEmpty(cityName)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return sysAirportService.getAirportByCityName(MapUtils.build("city", cityName));
    }

    @PostMapping(value = {"/map/around"})
    @ApiOperation(value = "获取周围司机", notes = "获取周围司机")
    public ResponseData<List<DriverPoint>> mapAround(@RequestHeader String appid,
                                                     @RequestBody ApiAroundParam aroundParam) {
        if (aroundParam.getBusinessType() == null) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        List<DriverPoint> driverPoints = locationService.geoNearOnline(aroundParam.getBusinessType(), aroundParam.getLng(),
                aroundParam.getLat(), appid, 8000, 100).getData();
        if (CollectionUtils.isNotEmpty(driverPoints)) {
            List<String> driverUuids = driverPoints.stream().map(driverPoint -> driverPoint.getDriverUuid()).distinct()
                    .collect(Collectors.toList());
            //过滤上班状态司机
            List<String> workDriverUuids = driverService.filterWorkUuid(driverUuids).getData();
            if (CollectionUtils.isNotEmpty(workDriverUuids)) {
                driverPoints = driverPoints.stream().filter(driverPoint -> workDriverUuids.contains(
                        driverPoint.getDriverUuid())).collect(Collectors.toList());
            }
        }
        return ResponseData.buildSuccessResponse("获取成功", driverPoints);
    }


    @PostMapping(value = {"/version/check"})
    @ApiOperation(value = "版本更新", notes = "版本更新")
    public ResponseData<AppVersionVo> versionCheck(@RequestBody Map<String, String> paramMap, @RequestHeader String appid) {
        // 平台（android/ios）
        String platform = paramMap.get("platform");
        // 版本号
        String versionNo = paramMap.get("versionNo");
        if (StringUtils.isEmpty(platform) || StringUtils.isEmpty(versionNo)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return commonVersionService.checkVersion(platform, versionNo, CommonConstant.PASSENGER, appid);
    }

    @PostMapping(value = {"/driInfo"})
    @ApiOperation(value = "乘客端获取司机信息详情接口", notes = "个人信息/星级评价/乘客评价")
    public ResponseData<Map<String, Object>> driInfo(@RequestBody ApiDriverUuidParam driverUuidParam) {
        if (StringUtils.isEmpty(driverUuidParam.getDriverUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return driverService.getDriverInfo(driverUuidParam.getDriverUuid());
    }
}
